const app = getApp();
import trigger from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeEnd: 0,
    timeStart: 0,
    isClick: false,
    current: 0,
    imgUrl: [],
    currentpage: 1,
    pagesize: 20,
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // (trigger.trigger)(this, '../terms/index')
    let that = this
    that.getPic()
    app.updateFlag(this).then(res => {
    })
  },
  getPic () {
    let that = this
    app.request(app.apiDomain + 'index.php?c=user&a=picture', {
      page: that.data.currentpage,
      size: that.data.pagesize
    }, function (res) {
      that.setData({
        imgUrl: res.data.data
      })
    })
  },
  authSuccess(e) {
    this.setData({
      isShow: false
    })
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  click() {
    this.setData({
      isShow: false
    })
  },
  // getUserInfo: function () {
  //   var that = this
  //   if (app.getUserInfo(that)) {
  //     that.setData({
  //       user_auth: false
  //     })
  //   }
  // },
  timestart: function (e) {
    var that = this
    that.setData({
      timeStart: e.timeStamp
    })
  },
  timeend: function (e) {
    var that = this
    that.setData({
      timeEnd: e.timeStamp
    })
  },
  saveImg: function (e) {
    let that = this
    console.log(e)
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      if (!!that.data.isClick) { return false }
      console.log('长按')
      wx.showLoading({
        title: '保存中',
      })
      wx.getSetting({
        success: function (res) {
          that.setData({
            isClick: true
          })
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function (resp) {
              let imgUrl = 'https://resource.160nurse.com/' + that.data.imgUrl[that.data.current].picture
              console.log(imgUrl)
              wx.showLoading({
                icon: '',
              })
              wx.downloadFile({
                url: imgUrl,
                success: function (res) {
                  wx.hideLoading()
                  // console.log(respon);
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (response) {
                      console.log(response)
                      that.setData({
                        isClick: false
                      })
                      wx.hideLoading()
                      wx.showToast({
                        title: '保存成功',
                        icon: 'success'
                      })
                    }
                  })
                }
              })
            },
            fail: function () {
              wx.hideLoading()
              that.setData({
                isClick: false
              })

            },
            complete: function (res) {
              // wx.hideLoading()
              console.log(res)
              that.setData({
                isClick: false
              })
            }
          })
        }
      })
    }
    
    // }
  },
  handleChange: function (e) {
    var that = this
    that.setData({
      current: e.detail.current
    })
  },
  onShareAppMessage: function () {
    var nickName = '';
    var userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }
    return {
      path: "/pages/terms/index?user_id=" + userId,
      title: '健康160精美壁纸，伴你度过每一个节日！'
      // imageUrl: app.resourceDomain + 'upload/miniprogram/runstep.png'
    }
  },
  return_index () {
   wx.reLaunch({
     url: '../index/index',
   })
  },
  return_act() {
    let pages = getCurrentPages()
    if (pages.length == 1) {
      wx.reLaunch({
        url: '../index/index',
      })
    } else {
      wx.reLaunch({
        url: '../personal/index',
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.SystemInfo(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
})